package com.zxl.live.screen.screen.ui.service;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.text.TextUtils;

import com.zxl.live.screen.screen.ui.helper.ScreenViewHelper;
import com.zxl.live.screen.screen.ui.helper.SuiScreenViewHelper;
import com.zxl.live.screen.screen.ui.receiver.ScreenReceiver;
import com.zxl.live.tools.track.StatisticsType;
import com.zxl.process.sdk.NotificationAssistService;

/**
 * Created by Administrator on 2017/11/8.
 */
public class ScreenService extends Service implements SuiScreenViewHelper.OnScreenStatusChangeListener {

    private ScreenReceiver mScreenReceiver;

    private ScreenViewHelper mScreenViewHelper;

    private SuiScreenViewHelper mSuiScreenViewHelper;

//    private KeyboardAdManager mKeyboardAdManager;

    @Override
    public void onCreate() {
        super.onCreate();
//        FullScreenManager.getInstance(this);
        mScreenReceiver = new ScreenReceiver();
        mScreenReceiver.registerReceiver(this);
        mScreenViewHelper = new ScreenViewHelper(this);
        mSuiScreenViewHelper = new SuiScreenViewHelper(this);
        mSuiScreenViewHelper.setOnScreenStatusChangeListener(this);
//        mKeyboardAdManager = new KeyboardAdManager();
//        mKeyboardAdManager.startAdListen();
    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        if (null != intent && !TextUtils.isEmpty(intent.getAction())) {
            mScreenViewHelper.onStartCommand(intent);
            mSuiScreenViewHelper.onStartCommand(intent);
        }
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
//        mKeyboardAdManager.stopAdListen();
        mScreenReceiver.unregisterReceiver(this);
//        FullScreenManager.getInstance(this).recycle();
    }

    @Override
    public void onDismiss() {
        mScreenViewHelper.reset();
    }

    /**
     * 内部服务，用于设置前台进程
     */
    public static class InnerService1 extends NotificationAssistService {
    }

    public static void startCoreService(Context context) {
        try {
            Intent service = new Intent(context, ScreenService.class);
            context.startService(service);
        } catch (Exception e) {
            e.printStackTrace();
            StatisticsType.DOWNLOAD_PRIVACY_APP.upload(context, "start_core_service_fail", e.getMessage());
        }
    }
}
